const SHORT_PROVINCE = {
  '台湾省': '台湾',
  '河北省': '河北',
  '山西省': '山西',
  '辽宁省': '辽宁',
  '吉林省': '吉林',
  '黑龙江省': '黑龙江',
  '江苏省': '江苏',
  '浙江省': '浙江',
  '安徽省': '安徽',
  '福建省': '福建',
  '江西省': '江西',
  '山东省': '山东',
  '河南省': '河南',
  '湖北省': '湖北',
  '湖南省': '湖南',
  '广东省': '广东',
  '海南省': '海南',
  '四川省': '四川',
  '贵州省': '贵州',
  '云南省': '云南',
  '陕西省': '陕西',
  '甘肃省': '甘肃',
  '青海省': '青海',
  '新疆维吾尔自治区': '新疆',
  '广西自治区': '广西',
  '内蒙古自治区': '内蒙古',
  '宁夏回族自治区': '宁夏',
  '西藏自治区': '西藏',
  '北京市': '北京',
  '天津市': '天津',
  '上海市': '上海',
  '重庆市': '重庆',
  '香港特别行政区': '香港',
  '澳门特别行政区': '澳门'
}

const shortProvince = p => SHORT_PROVINCE[p]
const rollPick = (list, idx) => list[idx % list.length]
const getPair = (list, repate) => list.map((i, idx) => [i, rollPick(repate, idx)])
const featureMatch = (f, i) => f.id === undefined ? f.name === i.name : f.id === i.id

const cache = new Map()
export const clearDataCache = () => cache.clear()
export const resolveDataCache = function (key, handle) {
  if (!cache.has(key)) {
    cache.set(key, handle())
  }
  return cache.get(key)
}

export default {
  // 边框样式
  border (...args) {
    window.$.each(args, (index, value) => {
      const $obj = value
      $obj.css({'position': 'relative'})
      const presetCss = [{
        top: '0',
        left: '0',
        margin: '-2px 0 0 -2px',
        'border-color': '#b3f8ff',
        'border-width': '2px 0 0 2px',
        'border-top-left-radius': '3px'
      },
      {
        top: '0',
        right: '0',
        margin: '-2px -2px 0 0',
        'border-cColor': '#b3f8ff',
        'border-width': '2px 2px 0 0',
        'border-top-right-radius': '3px'
      },
      {
        bottom: '0',
        right: '0',
        margin: '0 -2px -2px 0',
        'border-color': '#b3f8ff',
        'border-width': '0 2px 2px 0',
        'border-bottom-right-radius': '3px'
      },
      {
        bottom: '0',
        left: '0',
        margin: '0 0 -2px -2px',
        'border-color': '#b3f8ff',
        'border-width': '0 0 2px 2px',
        'border-bottom-left-radius': '3px'
      }]
      let angels = '<div class="angels">'
      for (let i = 0; i < 4; i++) {
        angels += '<b style="position:absolute;width:30px;height:30px;border-style:solid;'
        for (let j in presetCss[i]) {
          angels += j + ':' + presetCss[i][j] + ';'
        }
        angels += '"></b>'
      }
      angels += '</div>'
      $obj.append(angels)
    })
  },
  // echarts屏幕缩放
  winResize (obj) {
    let resizeTimer = null
    window.addEventListener('resize', () => {
      if (resizeTimer) clearTimeout(resizeTimer)
      resizeTimer = setTimeout(() => {
        obj.resize()
      }, 500)
    })
  },
  // 去重
  excludeRepeat (oldArr, repeatKey) {
    const newArr = []
    for (let i = 0; i < oldArr.length; i++) {
      let flag = true
      for (let j = 0; j < newArr.length; j++) {
        if (oldArr[i][repeatKey] === newArr[j]) {
          flag = false
        }
      }
      if (flag) {
        newArr.push(oldArr[i][repeatKey])
      }
    }
    return newArr
  },
  // 计算所有相同属性数量
  compare ($obj, $key) {
    let compareObj = {}
    for (let i = 0, len = $obj.length; i < len; i++) {
      if ($obj[i][$key] !== '') {
        if (compareObj[$obj[i][$key]] === undefined) {
          compareObj[$obj[i][$key]] = 1
        } else {
          compareObj[$obj[i][$key]] = compareObj[$obj[i][$key]] + 1
        }
      }
    }
    return compareObj
  },
  // 排序
  compareSort ($arr, key, Asc) {
    // 根据属性排序
    function compareFunc (propertyName) {
      if (Asc) {
        return function (obj1, obj2) {
          var val1, val2
          val1 = obj1[propertyName]
          val2 = obj2[propertyName]
          if (val1 < val2) {
            return -1
          } else if (val1 > val2) {
            return 1
          } else {
            return 0
          }
        }
      } else {
        return function (obj1, obj2) {
          var val1, val2
          val1 = obj1[propertyName]
          val2 = obj2[propertyName]
          if (val1 < val2) {
            return 1
          } else if (val1 > val2) {
            return -1
          } else {
            return 0
          }
        }
      }
    }
    const copy = $arr.slice(0)
    return copy.sort(compareFunc(key))
  },
  unqi: (list) => [...new Set(list)],
  groupBy: (items, key) => items.reduce(
    (result, item) => ({
      ...result,
      [item[key]]: [
        ...(result[item[key]] || []),
        item
      ]
    }),
    {}
  ),
  sortBy: (items, key) => items.slice().sort((i, j) => i[key] - j[key]),
  copy: (value, defaultVal = {}) => Object.assign({}, defaultVal, value),
  shortProvince,
  rollPick,
  getPair,
  featureMatch
}
